<?php

/**
 * @copyright        2017 opencart.cn - All Rights Reserved
 * @author:          Sam Chen <sam.chen@opencart.cn>
 * @created:         2017-11-24 17:24:44
 * @modified by:     Sam Chen <sam.chen@opencart.cn>
 * @modified:        2017-11-24 17:55:50
 */

class ControllerMobileCustomCss extends Controller {
  public function index() {
    $path = 'cache/';
    $prefix = 'mobile-custom';

    $pattern = DIR_IMAGE . $path . $prefix . '.*.css';
    $files = glob($pattern);
    if (count($files) == 1) {
      return $path . basename($files[0]);
    } else {  // Remove all cache css when more than one cache file found
      array_map('unlink', glob($pattern));
    }

    $css = $this->config->get('gd_mobile_css');

    // Output css to cache file
    if (empty($css)) {
      return false;
    }

    $file = $prefix . '.' . strtolower(uniqid()) . '.css';
    $relative_path = $path . $file;

    $file = fopen(DIR_IMAGE . $relative_path, 'w');
    $comment = '/*[' . date('Y/m/d H:i:s') . '] This file is generated by code, DO NOT edit this file! */'. PHP_EOL;
    $output = $comment . html_entity_decode($css, ENT_QUOTES, 'UTF-8');
    fwrite($file, $output);
    fclose($file);
    return $relative_path;
  }
}
